package com.zhanghp.class07;

import java.util.Arrays;

/**
 * <h1>测试归并边界中间数</h1>
 *
 * @author zhanghp
 * @since 2024/2/2 9:57
 */
public class Boundary_MergeSort {
    // 测试左右边界
    public static void printprocess(int[] arr, int L, int mid, int R) {
        if (L >= mid || R <= mid) {
            return;
        }
        print(copy(arr, L, R));
        int LMid = getMid2(L, mid);
        int RMid = getMid2(mid + 1, R);
        printprocess(arr, L, LMid, mid);
        print(copy(arr, L, mid));
        printprocess(arr, mid + 1, RMid, R);
        print(copy(arr, mid + 1, R));
        System.out.println("下一组");
    }

    private static int getMid2(int L, int R) {
        return L + ((R - L) >> 1);
    }

    private static void print(int[] arr) {
        System.out.println(Arrays.toString(arr));
    }

    private static int[] copy(int[] arr, int L, int R) {
        if (arr == null || arr.length < 2) {
            return arr;
        }
        int[] newArr = new int[R - L + 1];
        int index = 0;
        for (int i = L; i <= R; i++) {
            newArr[index == R ? index : index++] = arr[i];

        }
        return newArr;
    }
}
